<template>
  <div>
    <!-- 面包屑导航条 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :replace="true" :to="{ path: '/Home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="13">
        <el-col :span="2">
          <el-button type="primary" round>添加角色</el-button>
        </el-col>
      </el-row>
      <!-- 数据面板 -->
      <el-table :data="tableDate" stripe border style="width: 100%">
        <!-- 展开列 -->
        <el-table-column label="具体权限" type="expand" width="80px">
          <template slot-scope="scope">
            <el-row :class="['bdbottom','textcenter']" v-for="(item) in scope.row.children" :key="item.id">
              <!-- 渲染一级权限 -->
              <el-col :span="5">
                <el-tag>{{ item.rightsName }}</el-tag>
                <i class="el-icon-arrow-right"></i>
              </el-col>
              <!-- 渲染二级权限 -->
              <el-col :span="19">
                <el-row :class="['textcenter']" v-for="(item1) in item.children" :key="item1.id">
                  <el-col :span="5">
                    <el-tag type="success">{{ item1.rightsName }}</el-tag>
                    <i class="el-icon-arrow-right"></i>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#">
        </el-table-column>
        <el-table-column prop="adminName" label="用户名">
        </el-table-column>
        <el-table-column prop="userDescription" label="角色描述">
        </el-table-column>
        <el-table-column prop="levels" label="权限等级" width="100px">
          <el-tag type="success">二级</el-tag>
        </el-table-column>
        <el-table-column prop="role" label="角色名称" width="100px">
        </el-table-column>
        <!-- 操作列 -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑角色</el-button>
            <el-button size="mini" type="warning" icon="el-icon-user" @click="showdialogVisible(scope.row._id)">分配权限</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="removeUser(scope.row._id)">删除角色</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryinfo.pageNum" :page-sizes="[5, 10, 15, 20]" :page-size="5" layout="total, sizes, prev, pager, next, jumper" :total="5">
      </el-pagination>
    </el-card>
    <!-- 权限分配对话框 -->
    <el-dialog title="权限分配" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <!-- 树形控件 -->
      <el-tree class="treetext" ref="treeRef" :data="rightslist" :props="defaultProps" show-checkbox node-key="id" :default-expand-all="true" :default-checked-keys="defkeys"></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisibleConfirm()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableDate: [],
      adminid: '',
      queryinfo: {
        query: '',
        pageNum: 1,
        pageSize: 5,
        levels: 2
      },
      dialogVisible: false,
      rightslist: [],
      defaultProps: {
        children: 'children',
        label: 'rightsName'
      },
      defkeys: [11, 12, 21, 22]
    }
  },
  created() {
    this.getuserlist()
  },
  methods: {
    // 处理分配权限
    async dialogVisibleConfirm() {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      var id = this.adminid
      var ret = await this.$http.get(`/roles/rights/${id}`, { params: { keys: keys } })
      console.log(ret.data)
      this.dialogVisible = false
    },
    // 展示权限列表框
    async showdialogVisible(id) {
      this.adminid = id
      this.dialogVisible = true

      var ret = await this.$http.get('/roles/rights')
      if (ret.data.err_code !== 0) {
        console.log('获取权限列表失败')
      }
      console.log(ret.data)
      this.rightslist = ret.data.data
    },
    // 获取用户列表
    async getuserlist() {
      var ret = await this.$http.get('/admins')
      if (ret.data.err_code !== 0) {
        console.log('获取管理员列表失败')
      }
      console.log(ret.data)
      this.tableDate = ret.data.data
    },
    handleSizeChange(newpagesize) {
      this.queryinfo.pageSize = newpagesize
      this.getuserlist()
    },
    handleCurrentChange(newpagenum) {
      this.queryinfo.pageNum = newpagenum
      this.getuserlist()
    },
    // 删除管理员
    removeUser(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        var ret = await this.$http.delete(`/users/delete/${id}`)
        if (ret.data.err_code !== 0) {
          this.$message.error('删除失败')
        }
        this.getuserlist()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch((err) => {
        console.log(err)
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    }
  }
}

</script>
<style>
.bdbottom {
  border-bottom: 1px solid #eee
}

.textcenter {
  display: flex;
  align-items: center;
}

</style>
